package geppetto.phraseHMM.phraseExtraction.extractedphrase;


import geppetto.cat.corpus.BilingualCorpus;
import geppetto.phraseHMM.WordTrie;
import geppetto.reordering.AbstractReorderingCalculator;

import java.io.PrintStream;



public class ExtractedPhrasePair {
	protected int _sourceId = -1;
	protected int _targetId = -1;
	protected int[] _sourcePhrase;
	protected int[] _targetPhrase;
	protected int sentenceNumber;
	protected byte sentenceSource;
	protected int[] foreingSentence;
	protected int[] sourceSentence;
	protected int sourceStartPos;
	protected int sourceEndPos;
	protected int foreignStartPos;
	protected int foreignEndPos;
	protected double score = 0;
	protected String alignmentInformation;
	protected SentenceInfo sentenceInfo;
	protected boolean isNull = false;
	protected boolean isStartNode = false;
	protected boolean isEndNode = false;

	public ExtractedPhrasePair(int[] source, int[] target, int sentenceNumber,
			byte sentenceSource, int[] foreingSentence, int[] sourceSentence,
			int sourceStartPos, int sourceEndPos, int foreignStartPos,
			int foreignEndPos) {
		super();
		_sourcePhrase = source;
		_targetPhrase = target;
		this.sentenceNumber = sentenceNumber;
		this.sentenceSource = sentenceSource;
		this.foreingSentence = foreingSentence;
		this.sourceSentence = sourceSentence;
		this.sourceStartPos = sourceStartPos;
		this.sourceEndPos = sourceEndPos;
		this.foreignStartPos = foreignStartPos;
		this.foreignEndPos = foreignEndPos;
	}
	
	public ExtractedPhrasePair(int[] target, int sentenceNumber,
			byte sentenceSource, int[] sourceSentence,
			int sourceStartPos, int sourceEndPos, int foreignStartPos,
			int foreignEndPos) {
		super();
		_sourcePhrase = new int[0];
		_targetPhrase = target;
		this.foreingSentence = new int[0];
		this.sentenceNumber = sentenceNumber;
		this.sentenceSource = sentenceSource;
		this.sourceSentence = sourceSentence;
		this.sourceStartPos = sourceStartPos;
		this.sourceEndPos = sourceEndPos;
		this.foreignStartPos = foreignStartPos;
		this.foreignEndPos = foreignEndPos;
		this.isNull = true;
	}

	public int[] getSourcePhrase() {
		return _sourcePhrase;
	}

	public void setSourcePhrase(int[] source) {
		_sourcePhrase = source;
	}

	public int[] getTargetPhrase() {
		return _targetPhrase;
	}

	public void setTargetPhrase(int[] target) {
		_targetPhrase = target;
	}

	public int getSentenceNumber() {
		return sentenceNumber;
	}

	public void setSentenceNumber(int sentenceNumber) {
		this.sentenceNumber = sentenceNumber;
	}

	public byte getSentenceSource() {
		return sentenceSource;
	}

	public void setSentenceSource(byte sentenceSource) {
		this.sentenceSource = sentenceSource;
	}

	public int[] getForeignSentence() {
		return foreingSentence;
	}

	public void setForeingSentence(int[] foreingSentence) {
		this.foreingSentence = foreingSentence;
	}

	public int[] getSourceSentence() {
		return sourceSentence;
	}

	public void setSourceSentence(int[] sourceSentence) {
		this.sourceSentence = sourceSentence;
	}

	public int getSourceStartPos() {
		return sourceStartPos;
	}

	public void setSourceStartPos(int sourceStartPos) {
		this.sourceStartPos = sourceStartPos;
	}

	public int getSourceEndPos() {
		return sourceEndPos;
	}

	public void setSourceEndPos(int sourceEndPos) {
		this.sourceEndPos = sourceEndPos;
	}

	public int getForeignStartPos() {
		return foreignStartPos;
	}

	public void setForeignStartPos(int foreignStartPos) {
		this.foreignStartPos = foreignStartPos;
	}

	public int getForeignEndPos() {
		return foreignEndPos;
	}

	public void setForeignEndPos(int foreignEndPos) {
		this.foreignEndPos = foreignEndPos;
	}
	
	public String stringPhraseExtraction(){
		String sourceS = "";
		for(int source : _sourcePhrase){
			sourceS += source + " ";
		}
		String targetS = "";
		for(int target : _targetPhrase){
			targetS += target + " ";
		}
		String phrase = sourceS + " ||| " + targetS +  " ||| ";
		ExtractedPhrasePairDataCalc[] dataCalcs = ExtractedPhrasePairDataManager.getDataValues().toArray(new ExtractedPhrasePairDataCalc[0]);
		for(int i = 0; i < dataCalcs.length; i++){
			ExtractedPhrasePairDataCalc data = dataCalcs[i];
			phrase += data.calcData(this);
			if(i!=dataCalcs.length-1){
				phrase += " - ";
			}
		}
		if(getAlignmentInformation() != null){
			phrase += " ||| " + getAlignmentInformation();
		}
		return phrase;
	}

	public String stringPhraseExtraction(BilingualCorpus corpus){
		String sourceS  = getSourceString(corpus, _sourcePhrase);
		String targetS = getTargetString(corpus, _targetPhrase);
		String phrase = sourceS + " ||| " + targetS +  " ||| ";
		ExtractedPhrasePairDataCalc[] dataCalcs = ExtractedPhrasePairDataManager.getDataValues().toArray(new ExtractedPhrasePairDataCalc[0]);
		for(int i = 0; i < dataCalcs.length; i++){
			ExtractedPhrasePairDataCalc data = dataCalcs[i];
			phrase += data.calcData(this);
			if(i!=dataCalcs.length-1){
				phrase += " - ";
			}
		}
		return phrase;
	}

	public static String getSourceString(BilingualCorpus corpus, WordTrie sourcePhrases,
			WordTrie foreignPhrases, int sourceId) {
		if (sourceId != -1) {
			int[] source = sourcePhrases.getPhraseIndexesById(sourceId);
			return getSourceString(corpus, source);
		} else {
			return "null";
		}
	}
	
	public static String getSourceString(BilingualCorpus corpus, int[] source){
		String sourceS = "";
		for (int i = 0; i < source.length; i++) {
			sourceS += corpus.getSourceWordById(source[i]);
			if(i!=source.length-1){sourceS+= " ";}
		}
		return sourceS;
	}
	
	public static String getTargetString(BilingualCorpus corpus, WordTrie sourcePhrases,
			WordTrie foreignPhrases, int targetId) {
		String targetS = "";
		int[] target = foreignPhrases.getPhraseIndexesById(targetId);
		for (int i = 0; i < target.length; i++) {
			targetS += corpus.getForeignWordById(target[i]);
			if(i!=target.length-1){targetS+= " ";}
		}
		return targetS;
	}
	
	public static String getTargetString(BilingualCorpus corpus, int target[]) {
		String targetS = "";
		for (int i = 0; i < target.length; i++) {
			targetS += corpus.getForeignWordById(target[i]);
			if(i!=target.length-1){targetS+= " ";}
		}
		return targetS;
	}
	
	
	public String getTargetString(BilingualCorpus corpus) {
		return getTargetString(corpus, _targetPhrase);
	}
	
	public static String getHeaders(){
		String ret = "source" + " ||| " + "target" +  " ||| ";
		for(String header : ExtractedPhrasePairDataManager.getHeaders()){
			ret += (header + " - ");
		}
		return ret;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	public String getAlignmentInformation() {
		return alignmentInformation;
	}

	public void setAlignmentInformation(String alignmentInformation) {
		this.alignmentInformation = alignmentInformation;
	}

	public SentenceInfo getSentenceInfo() {
		return sentenceInfo;
	}

	public void setSentenceInfo(SentenceInfo sentenceInfo) {
		this.sentenceInfo = sentenceInfo;
	}
	
	public boolean isNullPhrase(){
		return isNull;
	}

	public boolean isStartNode() {
		return isStartNode;
	}

	public void setStartNode(boolean isStartNode) {
		this.isStartNode = isStartNode;
	}

	public boolean isEndNode() {
		return isEndNode;
	}

	public void setEndNode(boolean isEndNode) {
		this.isEndNode = isEndNode;
	}

	public int get_sourceId() {
		return _sourceId;
	}

	public void set_sourceId(int id) {
		_sourceId = id;
	}

	public int get_targetId() {
		return _targetId;
	}

	public void set_targetId(int id) {
		_targetId = id;
	}
	
	
}
